package com.maoye.ossp.biz.statement.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.db.api.IdGenerator;
import com.maoye.ossp.biz.statement.entity.BizStatementBillDetail;
import com.maoye.ossp.biz.statement.entity.BizStatementPaymentDetail;
import com.maoye.ossp.biz.statement.entity.BizStatementStmCounter;
import com.maoye.ossp.biz.statement.entity.BizStatementStmError;
import com.maoye.ossp.biz.statement.entity.BizStatementStmPrepaid;
import com.maoye.ossp.biz.statement.service.IBizStatementBillDetailService;
import com.maoye.ossp.biz.statement.service.IBizStatementPaymentDetailService;
import com.maoye.ossp.biz.statement.service.IBizStatementStmCounterService;
import com.maoye.ossp.biz.statement.service.IBizStatementStmErrorService;
import com.maoye.ossp.biz.statement.service.IBizStatementStmPrepaidService;
import com.maoye.ossp.biz.statement.util.BizStatementConstant;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.service.user.UserServiceImpl;

/**
 * 结算对账-异常 Dao
 * 
 * @author yangyf
 * 
 *         2014年2月18日
 */
@Controller
@RequestMapping(value = "/biz/statement/stm/error/")
public class BizStatementStmErrorController extends BaseController {
	@Resource
	private IBizStatementStmErrorService stmErrorService;

	@Resource
	private IBizStatementStmCounterService statementStmCounterService;

	@Resource
	private IBizStatementStmPrepaidService stmPrepaidService;

	@Resource
	private IdGenerator idGenerator;
	
	@Resource
	private UserServiceImpl userServiceImpl;
	
	@Resource
	private IBizStatementPaymentDetailService paymentDetailService;
	
	@Resource
	private IBizStatementBillDetailService billDetailService;

	/**
	 * 界面显示
	 * 
	 * @param stmError
	 *            参数
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "list")
	public String list(BizStatementStmError stmError,
			HttpServletRequest request, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, stmError);
		Page<BizStatementStmError> page = (Page<BizStatementStmError>) stmErrorService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/statement/statementError/errorList", model,
				request);
	}

	/**
	 * 详细信息
	 * 
	 * @param billNo
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "view/{id}")
	public String view(@PathVariable String id,
			HttpServletRequest request, HttpServletResponse response,
			Model model, RedirectAttributes redirectAttributes) {
		BizStatementStmError stmCounterError = stmErrorService
				.get(id);
		model.addAttribute("stmCounterError", stmCounterError);
		return "/biz/statement/statementError/view";

	}

	/**
	 * 按条件查询异常结算对账信息 (提出日期 销售日期 供应商 是否确认)
	 * 
	 * @param statementStmError
	 *            参数
	 * @param model
	 * @param redirectAttributes
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getByStmError")
	public String getByStmError(BizStatementStmError statementStmError,
			Model model, RedirectAttributes redirectAttributes,
			HttpServletRequest request) {
		Map map = new HashMap();
		map.put("isConfirmed", statementStmError.getIsConfirmed());
		map.put("createTime", statementStmError.getCreateTime());
		map.put("date", statementStmError.getDate());
		map.put("supplierNo", statementStmError.getSupplierNo());

		List<BizStatementStmError> list = stmErrorService.getError(map);
		model.addAttribute("list", list);
		return "/biz/statement/counter/counterError";
	}

	/**
	 * 修改状态 确认异常
	 * 
	 * @param request
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "{id}/confirmError")
	public String confirmError(@PathVariable String id,
			HttpServletRequest request, RedirectAttributes redirectAttributes,
			Model model) {
		BizStatementStmError stmError = stmErrorService.getErrorBillNo(id);

		Map map = new HashMap();
		map.put("id", id);
		map.put("isConfirmed", BizStatementConstant.IS_CONFIRMED);
		stmErrorService.confirmError(map);
		List<BizStatementStmError> list = stmErrorService.getErrorList(map);
		addMessage(redirectAttributes, "确认异常成功");
		model.addAttribute("list", list);
		return "/biz/statement/counter/counterError";

	}

	/**
	 * 添加专柜异常信息
	 * 
	 * @param statementStmError
	 *            参数
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(BizStatementStmError statementStmError,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String billNo = request.getParameter("billNo");
		String statementType = request.getParameter("statementType");
		if (BizStatementConstant.STATEMENT_TYPE_1.equals(statementType)) {
			BizStatementStmCounter stmCounter = statementStmCounterService
					.getByBill(billNo);
			statementStmError.setSupplierNo(stmCounter.getSupplierNo());
			statementStmError.setSupplierName(stmCounter.getSupplierName());
			statementStmError.setErrorAmount(stmCounter.getActualCost());
			statementStmError
					.setStatementType(BizStatementConstant.STATEMENT_TYPE_1);
		} else {
			BizStatementStmPrepaid prepaid = stmPrepaidService.getPrepaidBill(billNo);
			statementStmError.setDate(prepaid.getEndTime());
			statementStmError.setSupplierNo(prepaid.getSupplierNo());
			statementStmError.setSupplierName(prepaid.getSupplierName());
			statementStmError.setErrorAmount(prepaid.getAmountPaid());
			statementStmError
					.setStatementType(BizStatementConstant.STATEMENT_TYPE_2);
		}
		statementStmError.setCreateTime(new Date());
		statementStmError.setIsConfirmed(BizStatementConstant.UN_CONFIRMED);
		statementStmError.setBillNo(billNo);
		User user = UserUtils.getUser();
		statementStmError.setCreateBy(user.getId().toString());
		statementStmError.setCreateByUserName(user.getName());
		model.addAttribute("statementStmError", statementStmError);
		return "/biz/statement/statementError/add";

	}

	/**
	 * 保存结算添加的异常信息
	 * 
	 * @param stmError
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(BizStatementStmError statementStmError,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Long id = idGenerator.getUId();
		statementStmError.setId(id);
		stmErrorService.create(statementStmError);
		String varId=request.getParameter("id");
        String billNo=request.getParameter("billNo");

		
        Long userId=UserUtils.getCurrentUserId();
		User user= userServiceImpl.getUserById(String.valueOf(userId));
			String flag="0";
			if(user!=null&& user.getUserType().equals("SUPPLIER")){
				 flag="1";
			}
		String rtn = "";
		if (BizStatementConstant.STATEMENT_TYPE_1.equals(statementStmError
				.getStatementType())) {
			/*BizStatementStmCounter stmCounter = statementStmCounterService
					.getByBill(statementStmError.getBillNo());*/
			BizStatementStmCounter stmCounter = statementStmCounterService.get(varId);
			model.addAttribute("flag",flag);
			model.addAttribute("stmCounter", stmCounter);
			rtn = "/biz/statement/counter/counterView";

		} else if (BizStatementConstant.STATEMENT_TYPE_2
				.equals(statementStmError.getStatementType())) {
			/*BizStatementStmPrepaid stmPrepaid = stmPrepaidService
					.getPrepaidBill(statementStmError.getBillNo());
			*/
			Map map=new HashMap();
			map.put("billNo",billNo);
			map.put("type","ZJ00");
			//根据billNo 获取扣款明细
			List<BizStatementPaymentDetail> paymentDetailList=paymentDetailService.getDetailList(map);
			//查看详细信息	
			BizStatementStmPrepaid stmPrepaid = stmPrepaidService.get(varId);
			//设置本期实付款     
			stmPrepaid.setQuasiPayment(stmPrepaid.getAmountPaid()+stmPrepaid.getPrPeriodAccountopen());
			//根据billNo 获取采购/调价明细
			List<BizStatementBillDetail> billDetailList=billDetailService.getBizStatementBillDetailList(map);
			model.addAttribute("flag",flag);
			model.addAttribute("paymentDetailList",paymentDetailList);
			model.addAttribute("billDetailList",billDetailList);
			model.addAttribute("stmPrepaid", stmPrepaid);
			rtn = "/biz/statement/prepaid/view";
		}
		model.addAttribute("active", "statementError");
		return rtn;
	}

	@RequestMapping(value = "showStatementErrorList")
	public String showStatementErrorList(BizStatementStmError stmError,
			HttpServletRequest request, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, stmError);
		Page<BizStatementStmError> page = (Page<BizStatementStmError>) stmErrorService
				.find(queryFilter);
		model.addAttribute("page", page);
		return "/biz/statement/statementError/showStatementErrorList";
	}
}
